import { createRouter, createWebHashHistory } from 'vue-router'
const routes = [
    {
        path: '/', redirect: '/login'
    },
    {
        path: '/login', component: () => import('../view/login.vue')
    },
    // 预约就诊
    {
        path: '/reservation', component: () => import('../view/PageHome.vue'),
        children: [
            {
                path: 'register', name: 'register', component: () => import('../view/Visit/Appointment.vue')
            },
            {
                path: 'record', name: 'record', component: () => import('../view/Visit/Record.vue')
            },
            {
                path: '/home', name: 'home', component: () => import('../view/Visit/index.vue')
            },
        ]
    },
    // 用户管理
    {
        path: '/usermanage', component: () => import('../view/PageHome.vue'),
        children: [
            {
                path: 'AdminInfo', name: 'AdminInfo', component: () => import('../view/admin/AdminInfo.vue')
            },
            {
                path: 'DoctorInfo', name: 'DoctorInfo', component: () => import('../view/admin/DoctorInfo.vue')
            },
            {
                path: 'UserInfo', name: 'UserInfo', component: () => import('../view/admin/UserInfo.vue')
            }
        ]
    },
    // 信息管理
    {
        path: '/information', component: () => import('../view/PageHome.vue'),
        children: [
            {
                path: 'Department', name: 'Department', component: () => import('../view/information/Department.vue')
            },
            {
                path: 'ArrangeDate', name: 'ArrangeDate', component: () => import('../view/information/ArrangeDate.vue')
            }
        ]
    },
    // 个人信息修改
    {
        path: '/User', component: () => import('../view/PageHome.vue'),
        children: [
            {
                path: 'Modify', name: 'Modify', component: () => import('../view/user/Modify.vue')
            },
            {
                path: 'Personal', name: 'Personal', component: () => import('../view/user/Personal.vue')
            }
        ]
    },
    {
        path: '/pay', component: () => import('../view/Visit/Pay.vue')
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const loggedInUser = JSON.parse(localStorage.getItem('loggedInUser'));
    // console.log("router", userStore.userInfo)

    if (!loggedInUser && to.path !== '/login') {
        // 如果没有登录信息，并且访问的不是登录页面，跳转到登录页面
        next('/login');
    }
    else {
        console.log("routerOK")
        next();
        // if (loggedInUser) {
        //     const userRole = loggedInUser.role;
        //     console.log("router",userRole)
        //     if (userRole === '0' && (to.path.includes('/usermanage') || to.path.includes('/information'))) {
        //         // 如果用户是普通用户并且尝试访问管理员页面，禁止访问
        //         next('/home');
        //     } else {
        //         next();
        //     }
        // } else {
        //     next();
        // }
    }
});

export default router
